<?php
/**
 * Author: dai
 * DateTime: 2016/11/3 14:36
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Log;

class Api
{
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        $data=[];
        $data['header'] = json_encode($request->header(),JSON_UNESCAPED_UNICODE);
        $data['request'] = json_encode($request->all(),JSON_UNESCAPED_UNICODE); //请求参数request
        $data['url'] = $request->fullUrl(); //请求url
        $data['response'] = $response->getContent(); //结果
        $data['api_method'] = $request->path(); //接口名称
        $data['operation_name'] = $request->offsetGet('header_operator'); //操作人
        $data['local_ip'] = $request->server->get('SERVER_ADDR', ''); //本机IP
        $data['request_ip'] = request()->ip(); //请求IP
        $data['http_status'] = $response->getStatusCode(); //状态码
        $data['http_method'] = $request->method(); //http状态
        $data['operation_id'] = $request->offsetGet('header_operator_id'); //操作人id
        $data['from'] = ""; //请求来源
        $data['type'] = "oms"; //所属系统
        $data['execution_time'] = microtime(true) - LARAVEL_START; //执行时间
        Log::info(json_encode($data,JSON_UNESCAPED_UNICODE));
    }
}